package cc.customer.web.sys.controller.system;

import cc.customer.common.annotation.Log;
import cc.customer.common.domain.ResponseBo;
import cc.customer.system.domain.CDingConfig;
import cc.customer.system.domain.SaleStage;
import cc.customer.system.domain.SaleTag;
import cc.customer.system.domain.TSeasConfig;
import cc.customer.system.service.DingConfigServices;
import cc.customer.system.service.SaleService;
import cc.customer.system.service.SaleTagService;
import cc.customer.web.sys.controller.base.BaseController;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class SaleController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(SaleController.class);

    @Autowired
    private SaleService salseService;

    @Autowired
    private SaleTagService saleTagService;
    @Autowired
    private DingConfigServices dingConfigServices;

    /**返回销售管理配置**/
    @RequestMapping("saleConfig/index")
    public String saleConfigIndex(){return "system/sale/sale_index";}

    /*加载新建标签*/
    @RequestMapping(value="saleConfig/loadCreateGrouping")
    public String loadCreateGrouping(){return "system/sale/sale_creategroup";}

    /*加载添加流程页面*/
    @RequestMapping("saleConfig/loadAddTrack")
    public String loadAddTrack(){
        return "system/sale/sale_addTrack";
    }

    /*加载创建标签页面*/
    @RequestMapping(value="saleConfig/loadCreateTag")
    public String loadCreateTag(){
        return "system/sale/sale_createTag";
    }

    @RequestMapping(value="saleConfig/loadUpdateGroup")
    public String loadUpdateGroup(){
        return "system/sale/sale_updateGroupName";
    }

    @RequestMapping(value="saleConfig/loadUpdateTag")
    public String loadUpdateTag(){
        return "system/sale/sale_updateTagName";
    }

    @RequestMapping("saleConfig/initTrackTable")
    @ResponseBody
    public ResponseBo initTrackTab(){
       List<SaleStage> list = salseService.selectAll();
       return ResponseBo.ok("success",list);
    }

    @Log("更新销售设置，客户阶段")
    @RequestMapping("saleConfig/updateSaleTrack")
    @ResponseBody
    public ResponseBo updateSaleName(SaleStage saleStage){
        try {
            int num = salseService.updateSale(saleStage);
            return ResponseBo.ok("更新成功","");
        }catch (Exception e){
            return ResponseBo.error("更新失败");
        }
    }

    /**获取公海配置文件数据**/
    @RequestMapping("SeasConfig/getSealConfig")
    @ResponseBody
    public ResponseBo getSealConfig(){
        try {
            List<TSeasConfig> list = salseService.getAllSeasConfig();
            return ResponseBo.ok(list);
        }catch (Exception e){
            return ResponseBo.error("更新失败");
        }
    }

    @Log("修改了公海资源配置")
    @RequestMapping("SeasConfig/updateSealConfig")
    @ResponseBody
    public ResponseBo updateSealConfig(TSeasConfig tSeasConfig){
        try {
            int num = salseService.updateSeasConfig(tSeasConfig);
            return num >0 ?ResponseBo.ok("更新成功"):ResponseBo.error("更新失败");
        }catch (Exception e){
            return ResponseBo.error("更新失败");
        }
    }


    @RequestMapping("saleConfig/createGrouping")
    @ResponseBody
    public ResponseBo createGrouping(SaleTag saleTag){
        try {
           int num = saleTagService.save(saleTag);
           return ResponseBo.ok("创建成功","");
        }catch (Exception e){
            log.info(e.getMessage());
            return ResponseBo.error("创建失败，请稍后再试");
        }
    }

    /*查标签分组是否已经存在*/
    @RequestMapping(value="saleConfig/checkGroupName")
    @ResponseBody
    public Boolean checkGroupName(SaleTag saleTag){
        boolean flag = true;
        SaleTag sale = saleTagService.selectByGroupName(saleTag);
        if(saleTag!=null){
            flag = false;
        }
        return flag;
    }

    /*查标签是否已经存在*/
    @RequestMapping(value="saleConfig/checkTagName")
    @ResponseBody
    public Boolean checkTagName(SaleTag saleTag){
        boolean flag = true;
        List<SaleTag> list = saleTagService.selectByTagName(saleTag);
        if(list.size()>0){
            flag = false;
        }
        return flag;
    }

    /*初始化标签分组列表*/
    @RequestMapping(value="saleConfig/getAllTagGroup")
    @ResponseBody
    public ResponseBo getAllTagGroup(){
        try{
            Map<String,Object> list = saleTagService.selectTagGroup();
            return ResponseBo.ok("scucess",list);
        }catch (Exception e){
            return ResponseBo.error();
        }
    }

    /*切换标签组*/
    @RequestMapping(value="saleConfig/chooseGroup")
    @ResponseBody
    public ResponseBo chooseGroup(String uuid){
        JSONObject list = saleTagService.chooseGroup(uuid);
        return ResponseBo.ok("切换成功",list);
    }

    /*删除标签分组*/
    @RequestMapping(value="saleConfig/deleteTagGroup")
    @ResponseBody
    public ResponseBo deleteTagGroup(SaleTag sale){
       try {
          int num = saleTagService.deleteGroup(sale);
          if(num==1){
              return ResponseBo.ok("删除成功","");
          }else{
              return ResponseBo.error("删除失败，当前分组下面包含标签");
          }

       }catch (Exception e){
           log.info(e.getMessage());
          return ResponseBo.error("删除失败");
       }
    }

    /*修改分组名称*/
    @RequestMapping(value="saleConfig/updateGroupName")
    @ResponseBody
    public ResponseBo updateGroupName(SaleTag sale){
        try {
            saleTagService.updateNotNull(sale);
            return ResponseBo.ok("更新成功","");
        }catch (Exception e){
            log.info(e.getMessage());
            return ResponseBo.error("更新失败，稍后再试");
        }
    }

    /*创建标签*/
    @RequestMapping(value="saleConfig/createTag")
    @ResponseBody
    public ResponseBo createTag(SaleTag saleTag){
        try {
            int num = saleTagService.saveTag(saleTag);
            return ResponseBo.ok("创建成功","");
        }catch (Exception e){
            log.info(e.getMessage());
            return ResponseBo.error("创建失败，请稍后再试");
        }
    }

    /*删除标签*/
    @RequestMapping(value="saleConfig/deleteTag")
    @ResponseBody
    public ResponseBo deleteTag(SaleTag saleTag){
        try {
            saleTagService.delete(saleTag);
            return ResponseBo.ok("删除成功");
        }catch (Exception e){
            log.info(e.getMessage());
            return ResponseBo.error("删除失败，请稍后再试");
        }
    }

    /*修改标签名*/
    @RequestMapping(value="saleConfig/updateTagName")
    @ResponseBody
    public ResponseBo updateTagName(SaleTag saleTag){
        try {
            saleTagService.updateNotNull(saleTag);
            return ResponseBo.ok("修改成功");
        }catch (Exception e){
            log.info(e.getMessage());
            return ResponseBo.error("修改失败，请稍后再试");
        }
    }
    /**获取标签得信息**/
    @RequestMapping(value="saleConfig/selectSaletag")
    @ResponseBody
    public ResponseBo selectSaletag(){
        try {
            JSONArray json  = saleTagService.getAllTag();
            return ResponseBo.ok(json);
        }catch (Exception e){
            log.info(e.getMessage());
            return ResponseBo.error("获取失败，请稍后再试");
        }
    }

    /**新增推送钉钉项目**/
    @RequestMapping(value="dingConfig/dingConfigAdd")
    @ResponseBody
    public ResponseBo dingConfigAdd(CDingConfig cDingConfig){
        return dingConfigServices.addOrUpdateConfg(cDingConfig);
    }

    /**查询推送钉钉项目**/
    @RequestMapping(value="dingConfig/initExName")
    @ResponseBody
    public ResponseBo initExName(CDingConfig config){
        return dingConfigServices.selectConfig(config);
    }

    /**删除项目**/
    @RequestMapping(value="dingConfig/deleteExName")
    @ResponseBody
    public ResponseBo deleteExName(String exName,String type){
	    return dingConfigServices.deleteSelectInter(exName,type);
    }

    /**获取部门的人员**/
    @RequestMapping(value="dingConfig/getUserInfoById")
    @ResponseBody
    public ResponseBo getUserInfoById(String internet,String deptid,String type){
        return dingConfigServices.selectDingUserByDeptid(internet,deptid,type);
    }

    /**保存推送项目**/
    @RequestMapping(value="dingConfig/saveCdingConfig")
    @ResponseBody
    public ResponseBo getUserInfoById(CDingConfig cDingConfig){
        return dingConfigServices.saveDingUserConfig(cDingConfig);
    }


    /**获取部门的人员**/
    @RequestMapping(value="dingConfig/getDingUserByDeptid")
    @ResponseBody
    public ResponseBo getDingUserByDeptid(String deptid,String type){
        return dingConfigServices.selectDingByDept(deptid,type);
    }


}
